package com.example.Views;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import functions.item;


import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;



import java.util.ArrayList;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    functions.Home home_fct;

    public MyActivity() {
        home_fct = new functions.Home(this);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void new_item(View view) {
        setContentView(R.layout.new_item);
    }

    public  void scan_code(View v){
        //check for scan button
        if(v.getId()==R.id.button4){
            //instantiate ZXing integration class
            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            //start scanning
            scanIntegrator.initiateScan();
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //retrieve result of scanning - instantiate ZXing object
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        //invalid scan data or scan canceled
        Toast toast3 = Toast.makeText(getApplicationContext(),
                "sku!", Toast.LENGTH_SHORT);
        toast3.show();
        //check we have a valid result
        if (scanningResult != null) {
            Toast toast2 = Toast.makeText(getApplicationContext(),
                    "sku2!", Toast.LENGTH_SHORT);
            toast2.show();
            //get content from Intent Result
            String scanContent = scanningResult.getContents();
            //get format name of data scanned
            String scanFormat = scanningResult.getFormatName();
            //output to UI
            //formatTxt.setText("FORMAT: "+scanFormat);
            //contentTxt.setText("CONTENT: "+scanContent);
            Log.d("my code format", scanFormat);
            Log.d("my code content", scanContent);
            Toast toast4 = Toast.makeText(getApplicationContext(),
                    scanContent, Toast.LENGTH_SHORT);
            toast4.show();
            home_fct.decrease(scanContent);
        }
        else{
            //invalid scan data or scan canceled
            Toast toast = Toast.makeText(getApplicationContext(),
                    "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    /* When "add product clicked" */
    public void add_prd(View view) {
        String SKU;
        String Name;
        Integer Stock;
        item myitem;
        String path;

        //Get data from view
        Name = ((EditText)findViewById(R.id.editText)).getText().toString();
        SKU = ((EditText)findViewById(R.id.editText1)).getText().toString();
        Stock = Integer.parseInt(((EditText) findViewById(R.id.editText2)).getText().toString());
        //Call the controller function
        path = home_fct.new_item(SKU, Name, Stock);
        /*Intent nextScreen = new Intent(getApplicationContext(), barcode_gen.class);
        nextScreen.putExtra("product_sku",myitem.getSku());
        startActivity(nextScreen);
        */
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("application/image");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"bastienschneider@gmail.com"});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App");
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + path));
        try {
            startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

    }

    public void see_all(View view){
        ListView lv;
        ArrayList  myitems;
        myitems = home_fct.show_all();


        setContentView(R.layout.show_all);

        lv = (ListView) findViewById(R.id.listView);
        ArrayAdapter<String> arrayAdapter;
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myitems);
        lv.setAdapter(arrayAdapter);

    }
}
